<?php

function getBaiduIndexCount($domain, $timecache = 1600000) {

    $arrDomainInfo = explode('.', $domain);
    foreach ($arrDomainInfo as $n) {
        if (is_numeric($n)) {
            return 'format_error';
        }
    }
    $length = count($arrDomainInfo);
    $maindomain = $arrDomainInfo[$length - 2] . '.' . $arrDomainInfo[$length - 1];

    $CacheFile = dirname(__FILE__) . '/data/domain-se/' . substr($maindomain, 0, 2) . '/' . $maindomain . '.' . __FUNCTION__ . '.txt';
    FCheckDir($CacheFile);
    if (file_exists($CacheFile)) {
        $arrData = json_decode(file_get_contents($CacheFile), true);
        ksort($arrData);
    }
    if (file_exists($CacheFile) && time() - filemtime($CacheFile) < $timecache) {
        $Index = 0;
        foreach ($arrData as $day => $item) {
            $Index++;
            if ($Index == count($arrData)) {
                log::n('cache get index count:' . $item);
                return $item;
            }
        }
    }

    $url = 'http://www.baidu.com/s?wd=site%3A' . $maindomain . '&rsv_bp=0&ch=&tn=baidu&bar=&rsv_spt=3&ie=utf-8&rsv_n=2&rsv_sug3=1&rsv_sug1=1&rsv_sug4=11&inputT=328';
    $s = curl_string($url);
    $p = '#<strong>找到相关结果数(.*)个。</strong>#isU';
    preg_match_all($p, $s, $arrIndex);
    $res = intval(str_replace(',', '', trim($arrIndex[1][0])));
    $arrData[date('Y-m-d')] = $res;
    file_put_contents($CacheFile, json_encode($arrData));
    log::n('realtime get index count:' . $res);
    return $res;
}

function get360IndexCount($domain, $timecache = 1600000) {
    $arrDomainInfo = explode('.', $domain);
    foreach ($arrDomainInfo as $n) {
        if (is_numeric($n)) {
            return 'format_error';
        }
    }
    $length = count($arrDomainInfo);
    $maindomain = $arrDomainInfo[$length - 2] . '.' . $arrDomainInfo[$length - 1];

    $CacheFile = dirname(__FILE__) . '/data/domain-se/' . substr($maindomain, 0, 2) . '/' . $maindomain . '.' . __FUNCTION__ . '.txt';
    FCheckDir($CacheFile);
    if (file_exists($CacheFile)) {
        $arrData = json_decode(file_get_contents($CacheFile), true);
        ksort($arrData);
    }
    if (file_exists($CacheFile) && time() - filemtime($CacheFile) < $timecache) {
        $Index = 0;
        foreach ($arrData as $day => $item) {
            $Index++;
            if ($Index == count($arrData)) {
                log::n('cache get index count:' . $item);
                return $item;
            }
        }
    }
    $url = 'http://www.so.com/s?q=site%3A' . $maindomain;
    $s = curl_string($url);
    $p = '#<p class="ws-total">找到相关结果约(.*)个</p>#isU';
    preg_match_all($p, $s, $arrIndex);
    $res = intval(str_replace(',', '', trim($arrIndex[1][0])));
    $arrData[date('Y-m-d')] = $res;
    file_put_contents($CacheFile, json_encode($arrData));
    log::n('realtime get index count:' . $res);
    return $res;
}

function getSogouIndexCount($domain, $timecache = 1600000) {
    $arrDomainInfo = explode('.', $domain);
    foreach ($arrDomainInfo as $n) {
        if (is_numeric($n)) {
            return 'format_error';
        }
    }
    $length = count($arrDomainInfo);
    $maindomain = $arrDomainInfo[$length - 2] . '.' . $arrDomainInfo[$length - 1];

    $CacheFile = dirname(__FILE__) . '/data/domain-se/' . substr($maindomain, 0, 2) . '/' . $maindomain . '.' . __FUNCTION__ . '.txt';
    FCheckDir($CacheFile);
    if (file_exists($CacheFile)) {
        $arrData = json_decode(file_get_contents($CacheFile), true);
        ksort($arrData);
    }
    if (file_exists($CacheFile) && time() - filemtime($CacheFile) < $timecache) {
        $Index = 0;
        foreach ($arrData as $day => $item) {
            $Index++;
            if ($Index == count($arrData)) {
                log::n('cache get index count:' . $item);
                return $item;
            }
        }
    }
    $url = 'http://www.sogou.com/web?query=site%3A' . $maindomain . '&_asf=www.sogou.com&_ast=1384510059&w=01019900&p=40040100&ie=utf8&sut=5351&sst0=1384510058710&lkt=21%2C1384510053361%2C1384510058613';
    $s = curl_string($url);
    $p = '#class="zz_ico">找到约 <em>(.*)</em> 条结果</h3>#isU';
    preg_match_all($p, $s, $arrIndex);
    $res = intval(str_replace(',', '', trim($arrIndex[1][0])));
    $arrData[date('Y-m-d')] = $res;
    file_put_contents($CacheFile, json_encode($arrData));
    log::n('realtime get index count:' . $res);
    return $res;
}

function getGoogleIndexCount($domain, $timecache = 1600000) {
    $arrDomainInfo = explode('.', $domain);
    foreach ($arrDomainInfo as $n) {
        if (is_numeric($n)) {
            return 'format_error';
        }
    }
    $length = count($arrDomainInfo);
    $maindomain = $arrDomainInfo[$length - 2] . '.' . $arrDomainInfo[$length - 1];

    $CacheFile = dirname(__FILE__) . '/data/domain-se/' . substr($maindomain, 0, 2) . '/' . $maindomain . '.' . __FUNCTION__ . '.txt';
    FCheckDir($CacheFile);
    if (file_exists($CacheFile)) {
        $arrData = json_decode(file_get_contents($CacheFile), true);
        ksort($arrData);
    }
    if (file_exists($CacheFile) && time() - filemtime($CacheFile) < $timecache) {
        $Index = 0;
        foreach ($arrData as $day => $item) {
            $Index++;
            if ($Index == count($arrData)) {
                log::n('cache get index count:' . $item);
                return $item;
            }
        }
    }
    $url = 'http://www.google.com.hk/search?newwindow=1&safe=strict&site=&source=hp&q=site:' . $maindomain . '&oq=site:qq.com&gs_l=hp.3...7855.13892.0.14061.18.14.0.0.0.0.0.0..0.0....0...1c.1.31.hp..18.0.0.12NpDiEoIHw&bav=on.2,or.&bvm=bv.56643336%2Cd.aGc%2Cpv.xjs.s.en_US.Z89Q2_x0q-0.O&fp=2239c49bb88f7a90&biw=1366&bih=171&dpr=1&tch=3&ech=1&psi=uPWFUtzsN4TmiAfLhYDYAg.1384510899711.3&wrapid=tlif138451091383511';
    $s = curl_string($url);
    #echo $s;
    $p = '#找到约(.*)条结果#isU';
    preg_match_all($p, $s, $arrIndex);
    if (!isset($arrIndex[1][0])) {
        $p = '#About(.*)results#isU';
        preg_match_all($p, $s, $arrIndex);
    }
    #約有 1,390,000,000 項結果
    if (!isset($arrIndex[1][0])) {
        $p = '#約有(.*)項結果#isU';
        preg_match_all($p, $s, $arrIndex);
    }
    $res = intval(str_replace(',', '', trim($arrIndex[1][0])));
    $arrData[date('Y-m-d')] = $res;
    file_put_contents($CacheFile, json_encode($arrData));
    log::n('realtime get index count:' . $res);
    return $res;
}

function domain_fset($domain, $type, $data) {
    $arr = explode('.', $domain);
    $length = count($arr);
    $extk = $length - 1;
    $dok = $length - 2;
    $maindomain = $arr[$dok] . '.' . $arr[$extk];

    $dir = substr(md5($maindomain), 0, 2);
    $cacheFile = dirname(__FILE__) . '/data/domain-money/' . $dir . '/' . $domain . '.' . $type . '.txt';
    FCheckDir($cacheFile);
    file_put_contents($cacheFile, json_encode($data));
}

function domain_fget($domain, $type) {
    $arr = explode('.', $domain);
    $length = count($arr);
    $extk = $length - 1;
    $dok = $length - 2;
    $maindomain = $arr[$dok] . '.' . $arr[$extk];

    $dir = substr(md5($maindomain), 0, 2);
    $cacheFile = dirname(__FILE__) . '/data/domain-money/' . $dir . '/' . $domain . '.' . $type . '.txt';
    FCheckDir($cacheFile);
    $json = file_get_contents($cacheFile);
    return json_decode($json, true);
}

function get_domain_usd($sub_or_main_domain, $type = '7day_1') {
    $arrUsdPvInfo = domain_fget($sub_or_main_domain, 'usdpv');
    return $arrUsdPvInfo[$type]['usd'];
}

function get_domain_clicks($sub_or_main_domain, $type = '7day_1') {
    $arrUsdPvInfo = domain_fget($sub_or_main_domain, 'usdpv');
    return $arrUsdPvInfo[$type]['clicks'];
}

function get_domain_pv($sub_or_main_domain, $type = '7day_1') {
    $arrUsdPvInfo = domain_fget($sub_or_main_domain, 'usdpv');
    return $arrUsdPvInfo[$type]['pv'];
}

function get_main_domains_by_subdomains($arrDomain) {
    $arrResult = array();
    foreach ($arrDomain as $domain) {
        $arr = explode('.', $domain);
        $length = count($arr);
        $extk = $length - 1;
        $dok = $length - 2;
        $domain = $arr[$dok] . '.' . $arr[$extk];
        if (is_numeric($arr[$extk])) {
            continue;
        }
        $arrResult[] = $domain;
    }
    return array_filter(array_unique($arrResult));
}

function get_all_installed_site_subdomains() {

    if (file_exists(dirname(__FILE__) . '/domainlist.txt')) {
        $s = file_get_contents(dirname(__FILE__) . '/domainlist.txt');
    }
    $domainlist = array_filter(explode('<br />', nl2br(trim($s))));
    foreach ($domainlist as &$ds) {
        $ds = preg_replace('#\s+#', '', $ds);
    }unset($ds);
    $domainlist = array_unique(array_filter($domainlist));
    foreach ($domainlist as $k => $item) {
        if (count($item) > 3) {
            unset($domainlist[$k]);
        }
    }

    $domainlist_1 = $domainlist;

    if (file_exists(dirname(__FILE__) . '/newdomainlist.txt')) {
        $s = file_get_contents(dirname(__FILE__) . '/newdomainlist.txt');
    }
    $domainlist = array_filter(explode('<br />', nl2br(trim($s))));
    foreach ($domainlist as &$ds) {
        $ds = preg_replace('#\s+#', '', $ds);
    }unset($ds);
    $domainlist = array_unique(array_filter($domainlist));
    foreach ($domainlist as $k => $item) {
        if (count($item) > 3) {
            unset($domainlist[$k]);
        }
    }

    $md = array_merge($domainlist, $domainlist_1);


	$files=array();

	for($i=1;$i<=10;$i++){
	$files[] = dirname(__FILE__) . '/input/csv/7day_'.$i.'.csv';
	$files[] = dirname(__FILE__) . '/input/csv/30day_'.$i.'.csv';
	}

	foreach($files as $file){
		$arr=getCsvShouRuData($file);
		$keys=array_keys($arr);
		$md=array_merge($md,$keys);
	}


    $arrUnSetDomains = getArrayFromFile(dirname(__FILE__) . '/unsetdomain.txt');
    foreach ($md as $k => $maindomain) {
        if (in_array($maindomain, $arrUnSetDomains)) {
            unset($md[$k]);
        }
        if (in_array(str_replace('www.', '', $maindomain), $arrUnSetDomains)) {
            unset($md[$k]);
        }
    }

    return $md;
}

function arrCmpTimeRemain($a, $b) {
    if ($a['days'] == $b['days']) {
        return 0;
    }
    return($a['days'] > $b['days']) ? 1 : -1;
}

function arrCmpVpsUsdCount($a, $b) {
    if ($a['7day'] == $b['7day']) {
        return 0;
    }
    return($a['7day'] > $b['7day']) ? -1 : 1;
}

function arrCmpUsdCount($a, $b) {
    if ($a['usd'] == $b['usd']) {
        return 0;
    }
    return($a['usd'] > $b['usd']) ? -1 : 1;
}

function getCsvShouRuDataBySonDomain($file) {
    $ShouRuData = array();
    if (file_exists($file)) {
        $fp = fopen($file, 'r');
        $INDEX = 0;
        while ($strline = fgets($fp)) {
            if (!$INDEX) {
                $INDEX++;
                continue;
            }
            $dataline = explode("\t", $strline);
            $dataline[0] = preg_replace('#\s+#', '', $dataline[0]);
            $arrDoamin = explode('.', $dataline[0]);
            if (count($arrDoamin) != 3) {
                continue;
            }
            $maindomain = $dataline[0];
            $ShouRuData[$maindomain]['pv']+=$dataline[1];
            $ShouRuData[$maindomain]['clicks']+=$dataline[2];
            $ShouRuData[$maindomain]['usd']+=$dataline[6];
            $ShouRuData[$maindomain]['domain'] = $maindomain;
            $INDEX++;
        }
        fclose($fp);
    }
    usort($ShouRuData, 'arrCmpUsdCount');
    $arrFinalData = array();
    foreach ($ShouRuData as $data) {
        $arrFinalData[$data['domain']] = $data;
    }
    return $arrFinalData;
}

/**
 * 分析一个文件里面的统计数据按主域名
 * @param type $file
 * @return type
 */
function getCsvShouRuData($file) {
    $ShouRuData = array();
    if (file_exists($file)) {
        $fp = fopen($file, 'r');
        $INDEX = 0;
        while ($strline = fgets($fp)) {
            if (!$INDEX) {
                $INDEX++;
                continue;
            }
            $dataline = explode("\t", $strline);
            $dataline[0] = preg_replace('#\s+#', '', $dataline[0]);
            $arrDoamin = explode('.', $dataline[0]);
            if (count($arrDoamin) != 3) {
                continue;
            }
            $maindomain = $arrDoamin[1] . '.' . $arrDoamin[2];
            $ShouRuData[$maindomain]['pv']+=$dataline[1];
            $ShouRuData[$maindomain]['clicks']+=$dataline[2];
            $ShouRuData[$maindomain]['usd']+=$dataline[6];
            $ShouRuData[$maindomain]['domain'] = $maindomain;
            $INDEX++;
        }
        fclose($fp);
    }
    usort($ShouRuData, 'arrCmpUsdCount');
    $arrFinalData = array();
    foreach ($ShouRuData as $data) {
        $arrFinalData[$data['domain']] = $data;
    }
    return $arrFinalData;
}

?>